<?php
if (!defined("DB2XML888_CLASS_PHS")) {
	define("DB2XML888_CLASS_PHS",1);
	require_once('Constant888.class.php');
	require_once('Config888.class.php');
	class DB2XML888 {
		private $areaCode;
		private $db_link;
		private $Constant888;

		public function DB2XML888($AREA_CODE) {			
			$this -> Constant888 = Constant888::instance();
			$dbStr = Config888::getValue("DatabaseInfo");			
			
			list($server,$user,$password,$db_name)= explode(':',$dbStr);
						
/*			$server=trim($server);
			$user=trim($user);
			$password=trim($password);
			$db_name=trim($db_name);
*/
	
			$this -> areaCode = $AREA_CODE;			
			try {				
				$this -> db_link = mysql_connect($server,$user,$password);							
				mysql_select_db($db_name,$this -> db_link);				
			}
			
			catch (Exception $err) {				
				echo $err -> getMessage();
			}
		}

		/**
		 *
		 * @param $XML_File the output (XML) file
		 * @param $date result on that date, e.g: "2009-09-08" YYYY-MM-DD
		 * @return boolean true if success , else false
		 */
		public function covert2XML($date,$XML_File)
		{
			$resultTblName = $this-> Constant888 -> value("LOTTO_RESULT_TABLE");
			$areaCode = $this -> areaCode;

			
				
			$outFile = fopen($XML_File,"w");
			
			//XML header
			fprintf($outFile,$this -> Constant888 ->value("XML_HEADER") );
			sscanf($date,"%d-%d-%d",$year,$month,$day);			
			$dateString = sprintf("%02d-%02d-%d",$day,$month,$year);
			fprintf($outFile,'date = "'.$dateString.'">'."\n");

			//Working with Database
			$query =
			"select AREA_CODE,RESULT_DATE,PRIZE_ID,RESULT_NUMBER".
			" from $resultTblName".
			" where".
			" AREA_CODE = $areaCode".
			" and RESULT_DATE = \"$date\"".
			" order by PRIZE_ID asc "
			;
			
			//echo $query;
			$result = mysql_query($query,$this -> db_link);
			while ($row = mysql_fetch_assoc($result)) {
				fprintf($outFile,'<giai value="'.$row["RESULT_NUMBER"].'"> </giai>'."\n");
			}
			fprintf($outFile,'</xsmb>'."\n".'</ketqua888>');
			fclose($outFile);
		}
	}
}
?>